﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SARUserControls.ChangJingCanshu.DialogFrames;

namespace SARUserControls
{
    /// <summary>
    /// ObjectInstance.xaml 的交互逻辑
    /// </summary>
    public partial class ObjectInstance : UserControl
    {
        public ObjectInstance()
        {
            this.InitializeComponent();
        }

        private void gridMask_MouseEnter(object sender, MouseEventArgs e)
        {
            this.gridMask.Opacity = 0.1;
        }

        private void gridMask_MouseLeave(object sender, MouseEventArgs e)
        {
            this.gridMask.Opacity = 0;
        }

        private string objectName;
        public string SARObjectName
        {
            get { return objectName; }
            set { objectName = value; }
        }

        private string strSource;
        public string SARImageSource
        {
            get
            {
                return strSource;
            }
            set
            {
                strSource = value;
                this.imgIcon.Source = new BitmapImage(new Uri(value, UriKind.Relative));
            }
        }

        private IDictionary<string, object> attrValues;

        public IDictionary<string, object> SARAttrValues
        {
            get { return attrValues; }
            set { attrValues = value; }
        }

        private Point postion;

        public Point SARPostion
        {
            get { return postion; }
            set
            {
                postion = value;
                updateDisplay();
            }
        }

        private double realWidth = 650;
        public double SARRealWidth
        {
            get { return realWidth; }
            set
            {
                realWidth = value;
                updateDisplay();
            }
        }

        private double realHeight = 230;
        public double SARRealHeight
        {
            get { return realHeight; }
            set
            {
                realHeight = value;
                updateDisplay();
            }
        }

        private void updateDisplay()
        {
            double top = 230 - (230 / realHeight * postion.Y);
            double left = 650 / realWidth * postion.X;
            this.Margin = new Thickness(left, top, 0, 0);
        }

        private Type pageType;
        public Type SARPageType
        {
            get { return pageType; }
            set { pageType = value; settingAttr.IsEnabled = true; }
        }


        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            DialogPageBase page = (DialogPageBase)Activator.CreateInstance(pageType);
            page.setValues(this.attrValues);
            SARDialog dialog = new SARDialog(page);
            dialog.OKClick += new OKClickHandler(dialog_OKClick);
            dialog.ShowDialog();
        }

        void dialog_OKClick(IDictionary<string, object> values)
        {
            attrValues = values;
        }

        private void deleteMe_Click(object sender, RoutedEventArgs e)
        {
            ((Grid)this.Parent).Children.Remove(this);            
        }

        private Point startPoint;
        private void UserControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            startPoint = e.GetPosition(null);
        }

        private void UserControl_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = startPoint - mousePos;

            if (e.LeftButton == MouseButtonState.Pressed &&
                (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            {
                ObjectInstance instance = this;
                DataObject dragData = new DataObject(instance);
                DragDrop.DoDragDrop(this, instance, DragDropEffects.Move);
            }
        }        
    }
}